﻿using System;
using System.Collections.Generic;
using System.IO;
using log4net;
using Truncation.Detector.Windows;

namespace Truncation.Detector.WinForm
{
    public class Entry : ProjectImpl
    {
        private static readonly ILog Log = LogManager.GetLogger("Main");
        private ISolutionImpl _solution = null;
        private Uri _baseFolder;
        public Entry(Microsoft.Build.Evaluation.Project project)
            : base(project)
        {
            _baseFolder = new Uri(Environment.CurrentDirectory + Path.DirectorySeparatorChar);
        }

        public override void Run(ISolutionImpl solution)
        {
            Log.InfoFormat("--> Start to parse Winform project: {0}", FullPath);
            this._solution = solution;

            foreach (var projectItem in MsBuildProject.GetItems("EmbeddedResource"))
            {
                var dependValue = projectItem.GetMetadataValue("DependentUpon");
                var resxFile = projectItem.EvaluatedInclude;
                if (!String.IsNullOrEmpty(dependValue) && dependValue.EndsWith(".cs"))
                {
                    string name = dependValue.Replace(".cs", ".resx");
                    if (Path.GetFileName(resxFile).Equals(name))
                    {
                        var resourceReader = new ResourceReader(Path.Combine(MsBuildProject.DirectoryPath, resxFile));
                        resourceReader.Run();
                    }
                }
            }
        }
    }
}
